package com.fengxing.ams.web.facade;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fengxing.ams.web.facade.intf.IQuizFacade;
import com.fengxing.ams.web.facade.mouble.quiz.IQuizDAO;
import com.fengxing.ams.web.facade.mouble.quiz.IQuizResultDAO;
import com.fengxing.ams.web.facade.mouble.quiz.IUserQuizDAO;
import com.fengxing.ams.web.facade.mouble.quiz.QuizResultVO;
import com.fengxing.ams.web.facade.mouble.quiz.QuizVO;
import com.fengxing.ams.web.facade.mouble.quiz.UserQuizVO;
import com.fengxing.ams.web.facade.mouble.user.UserVO;

@Service
public class QuizFacade implements IQuizFacade {

	@Autowired
	private IQuizDAO quizDAO;
	@Autowired
	private IQuizResultDAO quizResultDAO;
	@Autowired
	private IUserQuizDAO userQuizDAO;
	
	@Override
	public List<QuizVO> getAllQuizData() {
		List<QuizVO> vos = new ArrayList<QuizVO> (48);
		Iterator<QuizVO> iter = this.quizDAO.findAll().iterator();
		while (iter.hasNext()) {
			vos.add(iter.next());
		}
		return vos;
	}

	@Override
	public QuizResultVO getQuizResult(String value) {
		return this.quizResultDAO.findOne(value);
	}

	@Override
	public void saveQuizResult(UserVO userInfo, String resultId,
			String resultTitle) {
		UserQuizVO quizVO = new UserQuizVO();
		quizVO.setCreated_date(new Date());
		quizVO.setQuiz_result(resultTitle);
		quizVO.setQuiz_title(resultId);
		quizVO.setUser_id(userInfo.getUserId());
		this.userQuizDAO.save(quizVO);
	}

	@Override
	public List<UserQuizVO> queryMyQuizResult(Long userId) {
		return this.userQuizDAO.findAllByUserId(userId);
	}

}
